package com.zhucx.utils;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import org.springframework.stereotype.Component;

/**
 * playwright  采集工具类
 *
 */
@Component
public class PlaywrightUtil {

    /**
     *
     * @param browser
     * @param mark
     * @param pageUrl
     * @param currentNum
     */
    public  void pageChuli(Browser browser,String mark,String pageUrl, int currentNum) {
        BrowserContext browserContext = browser.newContext();
        //每一页的地址
        Page page = browserContext.newPage();
        page.navigate(String.format(pageUrl, currentNum));
        Locator rows = page.locator("td a");

        int count = rows.count();

        for (int k = 0; k < count; k++) {
        }
        page.close();
        browserContext.close();
    }

    /**
     *
     * @param locator
     * @param begin  开始标志
     * @param end    结束标志
     * @return
     */
    public static String getType(Locator locator,String begin,String end){
        //示例链接  ../index.aspx?Db=chl&EncodingName=gb2312&gj=1&sort_id=10003&menuname=law
        String href=locator.getAttribute("href");
        int sortId= href.indexOf(begin);
        int menuName=href.indexOf(end);
        String code=href.substring(sortId,menuName).split("=")[1];
        return  code;
    }
}




